import { constantRoutes } from '@/router'
// import { menuList } from '@/api/managerNav'
import Layout from '@/layout'
// import { notifyError } from '@/utils/message'
/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
  if (route.meta && route.meta.roles) {
    return roles.some((role) => route.meta.roles.includes(role))
  } else {
    return true
  }
}
/**
 * Use meta.role to determine if the current user has permission
 * 获取自定义链接
 */
function getasyncRoutes() {
  const syncRoutes = [
    {
      path: '/general',
      component: Layout,
      redirect: '/general',
      meta: {
        title: '常规管理',
        icon: 'el-icon-aim',
        roles: ['admin']
      },
      children: [
        {
          path: '/general/systems',
          name: 'Systems',
          component: () => import('@/views/general/systems/index'),
          meta: { title: '系统配置', icon: 'el-icon-setting' }
        },
        // {
        //   path: '/general/wxmenu',
        //   component: () => import('@/views/general/wxmenu'),
        //   name: 'WxMenu',
        //   meta: { title: '微信菜单', icon: 'el-icon-folder-opened' }
        // },
        {
          path: '/general/attachment/index',
          component: () => import('@/views/general/attachment/index'),
          name: 'Attachment',
          meta: { title: '附件管理', icon: 'el-icon-folder-opened' }
        }
      ]
    },
    {
      path: '/auth',
      component: Layout,
      redirect: '/auth',
      meta: {
        title: '权限管理',
        icon: 'dashboard',
        roles: ['admin', 'editor']
      },
      children: [
        {
          path: 'manager-nav',
          name: 'managerNav',
          noCache: true,
          component: () => import('@/views/manager-nav/index'),
          meta: { title: '菜单列表', icon: 'el-icon-menu' }
        },
        {
          path: 'manager-role',
          component: () => import('@/views/manager-role/index'), // Parent router-view
          name: 'managerRole',
          noCache: true,
          meta: { title: '角色管理', icon: 'el-icon-s-platform' }
        },
        {
          path: 'manager',
          noCache: true,
          component: () => import('@/views/manager/index'),
          name: 'managerIndex',
          meta: { title: '管理员管理', icon: 'el-icon-user' }
        },
        {
          path: '/manager-log',
          noCache: true,
          component: () => import('@/views/manager-log'),
          name: 'managerLog',
          meta: { title: '管理员日志', icon: 'el-icon-document' }
        }
      ]
    },
    {
      path: '/member-cards',
      component: Layout,
      hidden: true,
      redirect: '/member-cards',
      children: [
        {
          path: 'index',
          hidden: true,
          name: 'memberCardsIndex',
          component: () => import('@/views/member-cards/index'),
          meta: { title: '会员卡', icon: 'dashboard', hidden: true }
        }
      ]
    },
    // {
    //   path: '/coupons',
    //   component: Layout,
    //   redirect: '/coupons',
    //   children: [

    //   ]
    // },
    {
      path: '/scenic',
      component: Layout,
      redirect: '/scenic',
      children: [
        {
          path: 'index',
          name: 'scenicIndex',
          component: () => import('@/views/scenic/index'),
          meta: { title: '景区管理', icon: 'el-icon-video-camera-solid' }
        }
      ]
    },
    {
      path: '/scenicGoods',
      component: Layout,
      redirect: '/scenicGoods',
      meta: {
        title: '消费券管理',
        icon: 'el-icon-aim',
        roles: ['admin']
      },
      children: [
        {
          path: 'index',
          name: 'scenicGoods',
          component: () => import('@/views/scenic-goods/index'),
          meta: { title: '消费券列表', icon: 'el-icon-eleme' }
        },
        {
          path: '/coupons/index',
          name: 'couponsIndex',
          component: () => import('@/views/coupons/index'),
          meta: { title: '消费券记录', icon: 'el-icon-s-ticket' }
        },
        {
          path: 'data',
          name: 'goodsData',
          hidden: true,
          component: () => import('@/views/goods-data/index'),
          meta: { title: '获取商品接口数据', icon: 'el-icon-help' }
        }
      ]
    },
    {
      path: '/user',
      component: Layout,
      redirect: '/user',
      meta: {
        title: '会员管理',
        icon: 'el-icon-aim'
      },
      children: [
        {
          path: 'index',
          name: 'userIndex',
          component: () => import('@/views/users/index'),
          meta: { title: '会员列表', icon: 'el-icon-user' }
        },
        {
          path: 'userScore',
          name: 'userScore',
          hidden: true,
          component: () => import('@/views/user-score/index'),
          meta: { title: '会员积分管理', icon: 'el-icon-medal', hidden: true }
        }
      ]
    },
    // {
    //   path: '/userDatabase',
    //   component: Layout,
    //   redirect: '/userDatabase',
    //   children: [
    //     {
    //       path: 'index',
    //       name: 'userDatabase',
    //       component: () => import('@/views/userDatabase/index'),
    //       meta: { title: '员工管理', icon: 'el-icon-school' }
    //     }
    //   ]
    // },
    { path: '*', redirect: '/404', hidden: true }
  ]
  return syncRoutes
  // const li = await menuList().then(res => {
  //   if (res.code !== 1) {
  //     notifyError(res.msg || '获取操作菜单失败')
  //   }
  //   const data = res.data
  //   const apiRoutes = []
  //   data.forEach(element => {
  //     apiRoutes.push({
  //       path: element.url,
  //       component: Layout,
  //       redirect: element.url,
  //       name: 'excel' + element.id,
  //       meta: {
  //         title: element.title,
  //         icon: 'excel'
  //       },
  //       children: [
  //         {
  //           path: 'export-excel' + element.id,
  //           component: () => import('@/views/opening'),
  //           name: 'ExportExcel' + element.id,
  //           meta: { title: element.title }
  //         }]
  //     })
  //   })
  //   return syncRoutes
  //   //  return apiRoutes
  // })
  // console.log(li, '接口地址')
  // return li
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
  const res = []

  routes.forEach((route) => {
    const tmp = { ...route }
    if (hasPermission(roles, tmp)) {
      if (tmp.children) {
        tmp.children = filterAsyncRoutes(tmp.children, roles)
      }
      res.push(tmp)
    }
  })

  return res
}

const state = {
  routes: [],
  addRoutes: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  }
}

const actions = {
  generateRoutes({ commit }, roles) {
    return new Promise((resolve) => {
      let accessedRoutes = getasyncRoutes()
      const apiasyncRoutes = getasyncRoutes()
      if (roles.includes('admin')) {
        console.log('admin---')
        accessedRoutes = apiasyncRoutes || []
      } else {
        console.log('not-admin---')
        accessedRoutes = filterAsyncRoutes(apiasyncRoutes, roles)
      }

      commit('SET_ROUTES', accessedRoutes)
      resolve(accessedRoutes)
      // menuList().then(res => {
      //   if (res.code !== 1) {
      //     notifyError(res.msg || '获取操作菜单失败')
      //   }
      //   const data = res.data
      //   const opens = '@/views/opening'
      //   const apiRoutes = []
      //   data.forEach(element => {
      //     const childrens = []
      //     if (element.children) {
      //       element.children.forEach(childs => {
      //         childrens.push(
      //           {
      //             path: childs.router_path,
      //             component: () => import('@/views/' + childs.router_view),
      //             name: childs.router_name,
      //             meta: { title: childs.title, icon: childs.icon }
      //           }
      //         )
      //       })
      //     }
      //     const pathInfo = {
      //       path: element.router_path,
      //       component: Layout,
      //       redirect: element.router_path,
      //       name: element.router_name,
      //       meta: {
      //         title: element.title,
      //         icon: element.icon
      //       },
      //       children: childrens
      //     }
      //     const oneInfo = {
      //       path: '',
      //       component: Layout,
      //       redirect: element.router_path,
      //       meta: {
      //         title: element.title + 'test',
      //         icon: element.icon
      //       },
      //       children: [
      //         {
      //           path: element.router_path,
      //           name: element.router_name,
      //           component: () => import(opens), // + element.router_view
      //           meta: { title: element.title + 'test', icon: element.icon }
      //         }]
      //     }
      //     apiRoutes.push(element.children ? pathInfo : oneInfo)
      //   })
      //   console.log(apiRoutes)
      //   commit('SET_ROUTES', apiRoutes)
      //   resolve(apiRoutes)
      // })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
